package com.example.casper.Experiment2024.data;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import com.example.casper.Experiment2024.R;
import java.util.ArrayList;

public class RecordShowActivity extends AppCompatActivity {
    private final ArrayList<Uri> imageUris = new ArrayList<>();

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_record_show);
        ViewPager2 viewPager = findViewById(R.id.viewPagerShow);
        ImageAdapter imageAdapter = new ImageAdapter(imageUris);
        viewPager.setAdapter(imageAdapter);


        Intent intent = getIntent();
        if (intent != null) {
            // 从Intent中获取传递的数据ArrayList<String> ImageUriString = intent.getStringArrayListExtra("photo");
            ArrayList<String> ImageUriString = intent.getStringArrayListExtra("photo");
            if (ImageUriString != null) {
                for (String uriString : ImageUriString) {
                    imageUris.add(Uri.parse(uriString));
                }
                imageAdapter.notifyDataSetChanged();
            }
            String title = intent.getStringExtra("Title");
            if (null != title) {
                TextView latitudeEditText = findViewById(R.id.showlatitude);
                TextView longitudeEditText = findViewById(R.id.showlongitude);
                String text = intent.getStringExtra("description");
                TextView recordtitle= findViewById(R.id.ShowTitle);
                recordtitle.setText(title);
                TextView recordtext= findViewById(R.id.ShowViews);
                recordtext.setText(text);
                TextView recordlocationname=findViewById(R.id.showLocation);
                recordlocationname.setText(String.format("%s%s", getString(R.string.locate),intent.getStringExtra("locationname")));
                latitudeEditText.setText(String.format("%s%s", getString(R.string.latitude), intent.getDoubleExtra("latitude", 116.397477)));
                longitudeEditText.setText(String.format("%s%s", getString(R.string.longitude),intent.getDoubleExtra("longitude",39.908692)));
            }
        }
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
    public static class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
        private final ArrayList<Uri> imageUris;
        public ImageAdapter(ArrayList<Uri> imageUris) {
            this.imageUris = imageUris;
        }
        @NonNull
        @Override
        public ImageAdapter.ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.records_image_layout, parent, false);
            return new ImageViewHolder(view);
        }
        @Override
        public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
            Uri currentUri = imageUris.get(position);
            boolean contains = currentUri.toString().matches("^\\d+$");
            if(contains)
            {
                holder.imageView.setImageResource(Integer.parseInt(String.valueOf(currentUri)));
            }
            else holder.imageView.setImageURI(currentUri);

            // 设置点击事件
            holder.imageView.setOnClickListener(v -> {
                Intent intent = new Intent(holder.imageView.getContext(), FullScreenImageActivity.class);
                intent.putExtra("imageUri", currentUri.toString());
                holder.imageView.getContext().startActivity(intent);
            });
        }
        @Override
        public int getItemCount() {
            return imageUris.size();
        }
        static class ImageViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public ImageViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.imageView);

            }
        }
    }
}
